//
//  STTabBarController.swift
//  RouterPushProtocol
//
//  Created by Toj on 2/22/22.
//

import UIKit

class STTabBarController: UITabBarController {
    
    open override var shouldAutorotate: Bool {
        return selectedViewController?.shouldAutorotate ?? false
    }
    
    // 支持哪些屏幕方向
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return selectedViewController?.supportedInterfaceOrientations ?? .portrait
    }
    
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }
    
    // open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    // return .portrait
    // }
    
    private var _isMatch = false
    public var isMatch = false {
        didSet {
            // print("-->toj: new: \(isMatch)")
            // print("-->toj: old: \(_isMatch)")
            if _isMatch == isMatch { return }
            let curentVC = selectedViewController?.title ?? ""
            setupMatchViewControllers()
            guard let vcs_ = vcs else { return }
            var index = 0
            for i in 0..<vcs_.count {
                let vc = vcs_[i]
                guard let title = vc.title, title == curentVC  else { continue }
                index = i
                break
            }
            selectedIndex = index
            _isMatch = isMatch
        }
    }
    
    private var matchVC: UIViewController?
    private var vcs: [UIViewController]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        setupTabBar()
        setupViewControllers()
        //isMatch = true
    }
}

extension STTabBarController {
    
    private func setupTabBar() {
        
        // print("-->toj: setupTabBar")
        let appearance = UITabBar.appearance()
        appearance.shadowImage = UIImage()
        appearance.backgroundImage = UIImage()
        // 背景色
        appearance.backgroundColor = .white.tt_alpha(0.9)
        // 背景颜色, 暂时不起作用, 与backgroundColor有什么区别?
        appearance.barTintColor = appearance.backgroundColor
        // barStyle = .black: 那么系统栏就是 .lightContent 也就是白色
        // barStyle = .default: 那么系统栏就也是默认的黑色了
        appearance.barStyle = .black
        // 选中的颜色
        // appearance.tintColor = .orange
        // 半透明的
        appearance.isTranslucent = false

        tabBar.st_shadow(color: UIColor.black, opacity: 0.24, radius: 3, offset: CGSize(width: 0, height: -0.5))
    }
    
    private func setupViewControllers() {
        
        let hvc = setupRootViewController(vc: STHomeViewController(), title: "Home", image: "tabBarHome")
        
        let lvc = setupRootViewController(vc: STTVViewController(), title: "LiveTV", image: "tabBarTV")
        
        let mvc = setupRootViewController(vc: STMeViewController(), title: "Me", image: "tabBarMe")
        
        let tchvc = setupRootViewController(vc: STMatchViewController(), title: "Match", image: "tabBarMatch")
       matchVC = tchvc
        
        vcs = [mvc, hvc, tchvc, lvc]
        
        viewControllers = vcs
    }
    
    private func setupMatchViewControllers() {
        
        if isMatch {
            vcs?.insert(matchVC!, at: 1)
        }
        else {
            if let index = vcs?.firstIndex(of: matchVC!) {
                vcs?.remove(at: index)
            }
        }
        
        viewControllers = vcs
    }
    
    private func setupRootViewController(vc: UIViewController, title: String, image normalNamed: String, selectedImage selectedNamed: String = "") -> UIViewController {
        
        let image = UIImage(named: normalNamed)
        let selectedImage = selectedNamed.count > 0 ? UIImage(named: selectedNamed) : UIImage(named: normalNamed + "Selected")
        
        vc.tabBarItem.title = title
        vc.title = title
        vc.tabBarItem.image = image
        vc.tabBarItem.selectedImage = selectedImage
        let navVC = STNavigationController(rootViewController: vc)
        return navVC
    }
}
